﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using EHTWebsiteProject.Code.Entity;
using EHTWebsite.Code;

namespace EHTWebsiteProject.Code.BLL
{
    public class LienKetWebSiteProvider
    {
        /// <summary>
        /// ham insert lien ket website vao database
        /// </summary>
        /// <param name="theLienKet"></param>
        /// <param name="IsAdd"></param>
        public static void IULienKetWebSite(LienKetWebSite theLienKet, bool IsAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_UIPublishLienKetWebSite]";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ID", theLienKet.ID));
            command.Parameters.Add(new SqlParameter("@MotaOther", theLienKet.MotaOther));
            command.Parameters.Add(new SqlParameter("@MotaVN", theLienKet.MotaVN));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theLienKet.NumberOfOrder));
            command.Parameters.Add(new SqlParameter("@Publish", theLienKet.Publish));
            command.Parameters.Add(new SqlParameter("@Website", theLienKet.Website));
           
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// xoa lien ket theo ID
        /// </summary>
        /// <param name="ID"></param>
        public static void DeleteLienKetByID(Int32 ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_DeleteLienKetWebSiteByID]";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ID", ID));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// ham lay tat ca cac lien ket
        /// </summary>
        /// <returns></returns>
        public static DataSet GetLienKet()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetLienKetWebSite]";
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }

        /// <summary>
        /// ham lay lien ket theo ID
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static LienKetWebSite GetLienKetByID(Int32 ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_DeleteLienKetWebSiteByID]";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ID", ID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            LienKetWebSite theLienKet = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theLienKet = new LienKetWebSite();
                theLienKet.MotaVN = ds.Tables[0].Rows[0]["MotaVN"].ToString();
                theLienKet.MotaOther = ds.Tables[0].Rows[0]["MotaOther"].ToString();
                theLienKet.Website = ds.Tables[0].Rows[0]["Website"].ToString();              
                theLienKet.ID = Int32.Parse(ds.Tables[0].Rows[0]["ID"].ToString());

                theLienKet.NumberOfOrder = Int32.Parse(ds.Tables[0].Rows[0]["NumberOfOrder"].ToString());
                theLienKet.Publish = Boolean.Parse(ds.Tables[0].Rows[0]["Publish"].ToString());              
            }

            return theLienKet;
        }


        public static void ChangePublish(Int32 ID, Boolean TrangThai)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_ChangePublishLienKetWebSiteByID]";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ID", ID));
            command.Parameters.Add(new SqlParameter("@Publish", TrangThai));
            DataProvider.ExecuteNoneQuery(command);
        }
    }
}